import type { RouteRecordRaw } from "vue-router"
import { routerConfig } from "@/router/config"
import { registerNavigationGuard } from "@/router/guard"
import { createRouter } from "vue-router"
import { flatMultiLevelRoutes } from "./helper"

const Layouts = () => import("@/layouts/index.vue")

/**
 * @name 常驻路由
 * @description 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置唯一的 Name 属性
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layouts,
    meta: {
      hidden: true
    },
    children: [
      {
        path: ":path(.*)",
        component: () => import("@/pages/redirect/index.vue")
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/pages/error/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/pages/error/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/pages/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/",
    component: Layouts,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/pages/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页仪表盘",
          svgIcon: "dashboard",
          affix: true
        }
      }
    ]
  },

  {
    path: "/storage",
    component: Layouts,
    children: [
      {
        path: "",
        component: () => import("@/pages/storage/index.vue"),
        name: "storage",
        meta: {
          title: "储位管理",
          elIcon: "Grid"
        }
      }
    ]
  },
  {
    path: "/receive",
    component: Layouts,
    children: [
      {
        path: "",
        component: () => import("@/pages/receive/index.vue"),
        name: "receive",
        meta: {
          title: "接货方式管理",
          elIcon: "Ship"
        }
      }
    ]
  },
  {
    path: "/goodsNotice",
    component: Layouts,
    children: [
      {
        path: "",
        component: () => import("@/pages/goodsNotice/index.vue"),
        name: "goodsNotice",
        meta: {
          title: "进货通知",
          elIcon: "DocumentAdd"
        }
      }
    ]
  },
  {
    path: "/receiveRegister",
    component: Layouts,
    children: [
      {
        path: "",
        component: () => import("@/pages/receiveRegister/index.vue"),
        name: "receiveRegister",
        meta: {
          title: "收货登记",
          elIcon: "Postcard"
        }
      }
    ]
  },
  {
    path: "/review",
    component: Layouts,
    children: [
      {
        path: "",
        component: () => import("@/pages/review/index.vue"),
        name: "review",
        meta: {
          title: "装车复核",
          elIcon: "Finished"
        }
      }
    ]
  },
  {
    path: "/unload",
    component: Layouts,
    children: [
      {
        path: "",
        component: () => import("@/pages/unload/index.vue"),
        name: "unload",
        meta: {
          title: "外包卸货",
          elIcon: "ShoppingCart" 
        }
      }
    ]
  },
  {
    path: "/stock",
    component: Layouts,
    children: [
      {
        path: "",
        component: () => import("@/pages/stock/index.vue"),
        name: "stock",
        meta: {
          title: "库存余量",
          elIcon: "Box"
        }
      }
    ]
  },
  {
    path: "/vehicleManage",
    component: Layouts,
    children: [
      {
        path: "",
        component: () => import("@/pages/vehicleManage/index.vue"),
        name: "vehicleManage",
        meta: {
          title: "车辆管理",
          elIcon: "Van"
        }
      }
    ]
  },
  {
    path: "/inspect",
    component: Layouts,
    children: [
      {
        path: "",
        component: () => import("@/pages/inspect/index.vue"),
        name: "inspect",
        meta: {
          title: "检验管理",
          elIcon: "Check"
        }
      }
    ]
  },
  {
    path: "/zone",
    component: Layouts,
    children: [
      {
        path: "",
        component: () => import("@/pages/zone/index.vue"),
        name: "zone",
        meta: {
          title: "分区配置管理",
          elIcon: "Operation"
        }
      }
    ]
  },
  {
    path: "/warnAlar",
    component: Layouts,
    children: [
      {
        path: "",
        component: () => import("@/pages/warnAlar/index.vue"),
        name: "warnAlar",
        meta: {
          title: "告警处理中心",
          elIcon: "Bell"
        }
      }
    ]
  },
  {
    path: "/topology",
    component: Layouts,
    children: [
      {
        path: "",
        component: () => import("@/pages/topology/index.vue"),
        name: "topology",
        meta: {
          title: "拓扑结构图",
          elIcon: "Link"
        }
      }
    ]
  }
]

/**
 * @name 动态路由
 * @description 用来放置有权限 (Roles 属性) 的路由
 * @description 必须带有唯一的 Name 属性
 */
export const dynamicRoutes: RouteRecordRaw[] = [

]

/** 路由实例 */
export const router = createRouter({
  history: routerConfig.history,
  routes: routerConfig.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  try {
    // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    location.reload()
  }
}

// 注册路由导航守卫
registerNavigationGuard(router)
